#!/bin/bash
[$l1]="$name3"
USERS=
ID=0
user=`cat ./data.txt`
for u in $users
do
    USERS[$ID]=$u
    ID=$(($ID+1))
done
first_user=${USERS[0]}
echo $first_user

File=./xitong.txt
function add_user {
   while true
   do

     echo -n "enter name:"
     read name
     echo -n "enter gender:"
     read gender
     echo -n "enter age:"
     read age
     user_info="$name:$gender:$age"
     USERS[$ID]=$user_info
     ID=$(($ID+1))
     echo "there are ${#USERS[@]} users"   
     echo -n "do you wanna add more?y/n:"
     read answer
     if [ $answer == "n" ];then
          break
     fi
   done
}

function query_user {
 while true
 do
   echo -n "请输入查询学员名字："
   read name
  for x in ${USERS[*]}
   do
          y=`echo $x | awk -F":" '{print $1}'`
             if [ $y == "$name" ];then
                 echo $x
            fi
   done
         echo -n "do you wanna query more information?y/n:"
         read anwser
         if [ $anwser == "n" ];then
            break
         fi
 done
}
function update_user {
     echo "请输入需要更新的信息"
     read name
     for l in ${USERS[@]}
     do
          y=`echo $l | awk -F":" '{print $1}'`
          if [ $y == "$name" ];then
             echo -e "输入信息：\n"
          echo "姓名：性别：成绩"
          read name3
            USERS[$l1]="$name3"
                else
             l1=$(($jl1+1))
          fi
      done


}

while true
do
    echo "1. 添加学员"
    echo "2. 查询学员"
    echo "3. 更新学员"
    echo "4. 退出系统"

    echo -n "请选择:"
    read choice

    if [ $choice -eq 1 ]; then
        add_user
   elif [ $choice -eq 2 ]; then
        query_user
    elif [ $choice -eq 3 ]; then
        update_user
    elif [ $choice -eq 4 ]; then
      echo "do you wanna save the information?"
         read anwser
         if [ $anwser = "y" ];then
           cat /dev/null > $File
           for ((u=0;$u<$ID;u++))
           do
             echo ${USERS[$u]} >> $File
           done
           exit 0
         elif [ $anwser = "n" ];then
           exit 0
         fi
    else
        echo "错误的选择!!!"
    fi
done
